<?php
	session_start();
	require_once("utility.php");
	
	if(isset($_GET['type']))
		$type = $_GET['type'];
	else 
		$type = $_POST['type'];
	switch($type)
	{
		case 'newAnnouncement':
			$uid 		= $_SESSION['user_id'];
			$subject	= $_POST['subject'];
			$desc		= $_POST['description'];
			$cats 		= '';
			if(isset($_POST['cats']))
			{
				$cats	= $_POST['cats'];
				$cats	= implode(',',$cats);
				$cats	= $cats.',';
			}
			$image		= $_FILES['image'];
			date_default_timezone_set('Asia/Bangkok');
			$when		= date("Y-m-d H:i:s"); 
			utility::addAnnouncement($subject,$desc,$cats,$image,$uid,$when);
		break;
		case 'editAnnouncement':
			$subject	= $_POST['subject'];
			$desc		= $_POST['description'];
			$cats 		= '';
			if(isset($_POST['cats']))
			{
				$cats	= $_POST['cats'];
				$cats	= implode(',',$cats);
				$cats	= $cats.',';
			}
			$oldImage	= $_POST['oldImage'];
			$image		= $_FILES['image'];
			date_default_timezone_set('Asia/Bangkok');
			$when		= date("Y-m-d H:i:s"); 
			$status 	= 0;
			if(isset($_POST['status']))
				$status = $_POST['status'];
			$id 		= $_POST['id'];
			utility::editAnnouncement($subject,$desc,$cats,$oldImage,$image,$when,$status,$id);
		break;
		case 'newBusiness':
			$uid 		= $_SESSION['user_id'];
			$name		= $_POST['name'];
			$desc		= $_POST['description'];
			$cats		= '';
			if(isset($_POST['cats']))
			{
				$cats	= $_POST['cats'];
				$cats	= implode(',',$cats);
				$cats	= $cats.',';
			}
			$image		= $_FILES['image'];
			date_default_timezone_set('Asia/Bangkok');
			$when		= date("Y-m-d H:i:s"); 
			$person		= $_POST['person'];
			$phone		= $_POST['phone'];
			$email		= $_POST['email'];
			$www		= $_POST['www'];
			$address	= $_POST['address'];
			utility::addBusiness($name,$desc,$cats,$image,$uid,$when,$person,$phone,$email,$www,$address);
		break;
		case 'editBusiness':
			$name		= $_POST['name'];
			$desc		= $_POST['description'];
			$cats		= '';
			if(isset($_POST['cats']))
			{
				$cats	= $_POST['cats'];
				$cats	= implode(',',$cats);
				$cats	= $cats.',';
			}
			$image		= $_FILES['image'];
			$oldImage	= $_POST['oldImage'];
			$status 	= 0;
			if(isset($_POST['status']))
				$status = $_POST['status'];
			$id 		= $_POST['id'];
			date_default_timezone_set('Asia/Bangkok');
			$when		= date("Y-m-d H:i:s"); 
			$person		= $_POST['person'];
			$phone		= $_POST['phone'];
			$email		= $_POST['email'];
			$www		= $_POST['www'];
			$address	= $_POST['address'];
			utility::editBusiness($name,$desc,$cats,$oldImage,$image,$when,$person,$phone,$email,$www,$address,$status,$id);
		break;
		case 'signup':
			$name 		= $_POST['name'];
			$password	= $_POST['password'];
            $password	= md5($password);
			$email		= $_POST['email'];
			$sex		= $_POST['sex'];
			$number		= $_POST['number'];
			date_default_timezone_set('Asia/Bangkok');
			$when		= date("Y-m-d H:i:s"); 
			utility::addUser($name,$email,$number,$sex,$password,$when);
		break;
		case 'editUser':
			$name 		= $_POST['name'];
			$password	= $_POST['password'];
			$oldPw		= $_POST['oldPassword'];
			if($password != $oldPw)
            	$password	= md5($password);
			$email		= $_POST['email'];
			$sex		= $_POST['sex'];
			$number		= $_POST['number'];
			date_default_timezone_set('Asia/Bangkok');
			$when		= date("Y-m-d H:i:s"); 
			$id 		= $_POST['id'];
			if(isset($_POST['status']))
				$status 	= $_POST['status'];
			utility::editUser($name,$email,$number,$sex,$password,$when,$id,$status);
		break;
		case 'signin': // is having problem with COOOOOOOOOOOOOKIE 
			$name		= $_POST['name'];
			$password	= $_POST['password'];
			$password	= md5($password);
			utility::signin($name,$password);
		break;
		case 'logout':
			unset($_SESSION['user_name']);
			unset($_SESSION['user_id']);
			unset($_SESSION['admin']);
			utility::redirect('/bizbuddy.com','You have Logged Out!');
			//setcookie('user_name',$name,time() + 86400);
			//setcookie('user_pw',$password,time() - 86400);
		break;
		case 'getUsers':
			$user = $_GET['user'];
			$a = utility::getUser($user);
			if($a == null)
				echo 'available';
			else
				echo 'existed';
		break;
		case 'newCat':
			$newCat = $_GET['value'];
			date_default_timezone_set('Asia/Bangkok');
			$when		= date("Y-m-d H:i:s"); 
			utility::addCat($newCat,$when);
		break;
		case 'content':
			$method 		= $_GET['method'];
			$content_type 	= $_GET['content_type'];
			if($content_type == 'announcement' || $content_type == 'business')
			{
				$id				= $_GET['id'];
				$post 			= utility::$method($id,$content_type);
				utility::showContent($post[0]);
			}
			if($content_type == 'search')
			{
				$keyword = $_GET['keyword'];
				if($keyword != '')
				{
					if($method == 'getSuggestions')
					{
						utility::$method($keyword);
					}
					else if($method == 'getResults')
					{
						$search_type = '';
						if(isset($_GET['search_type']))
						{
							$search_type = $_GET['search_type'];
							//$search_type = implode(',',$search_type);
						}
						utility::$method($keyword,$search_type);
					}
				}
			}
		break;
		case 'action':
			$what = $_GET['what'];
			$from = $_GET['from'];
			$who  = $_GET['who'];
			
			if($what == 'delete')
			{
				utility::delete($from,$who);
			}
			else if($what == 'edit')
			{
				utility::edit($from,$who);
			}
			// i d't understand. when putting approve cond at top, edit n delete d't work :(
			else if($what == 1 || $what == 0)
			{
				utility::approve($what,$from,$who);
			}
			else
			{
			}
		break;
	}
?>